package com.fzfnn.scrapbook.mapper;

import com.fzfnn.scrapbook.entity.Journal;
import com.fzfnn.scrapbook.vo.SearchUserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface SearchMapper {

    @Select("SELECT " +
            "j.journal_id AS journalId, " +
            "j.title, j.content, " +
            "j.user_id AS userId, " +
            "j.updated_at AS updatedAt, " +
            "j.created_at AS createdAt, " +
            "j.img_url AS imgurl, " +
            "j.like_count AS likeCount," +
            "j.is_public AS isPublic," +
            "u.username, u.imgurl AS userAvatar, u.user_id AS userId " +
            "FROM journal j " +
            "JOIN user u ON j.user_id = u.user_id " +
            "WHERE title LIKE CONCAT('%', #{keyword}, '%') AND is_public = 1")
    List<Journal> searchByTitle(String keyword);


    @Select("SELECT user_id AS userId, username AS userName , imgurl AS userAvatar, email " +
            "FROM user " +
            "WHERE (username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR email LIKE CONCAT('%', #{keyword}, '%')) " +
            "AND status = 1")
    List<SearchUserVo> searchByUsernameOrEmail(String keyword);


    @Select("SELECT COUNT(*) FROM user_follow " +
            "WHERE follower_id = #{followerId} AND followed_id = #{followedId}")
    int isFollowing(@Param("followerId") Integer followerId,
                    @Param("followedId") Integer followedId);
}
